package com.airlines.system.controller;


import com.airlines.system.domain.SysFunction;
import com.airlines.system.domain.SysMenu;
import com.airlines.system.domain.SysRole;
import com.airlines.system.domain.SysUser;
import com.airlines.system.service.SysFunctionService;
import com.airlines.system.service.SysMenuService;
import com.airlines.system.service.SysRoleService;
import com.airlines.system.service.SysTypeService;
import com.airlines.system.vo.MenuAndFuncTreeNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限管理模块-系统角色控制器
 */
@Controller
@RequestMapping(value = "/api/system/role")
public class RoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysTypeService sysTypeService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysFunctionService sysFunctionService;


    /**
     * 进入角色管理页面
     */
    @RequestMapping
    public String toPage(Model model) {
        model.addAttribute("types", sysTypeService.findAll());
        return "system/accessManagement/roleManage";
    }

    /**
     * 获取所有角色列表或者根据系统获取角色列表
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public List<SysRole> findAllRole(@RequestParam(required = false) String typeId) {

        List<SysRole> roles = new ArrayList<>();
        if (StringUtils.isEmpty(typeId)) {
            roles = sysRoleService.findAll();
        } else {
            roles = sysRoleService.findAllByType(typeId);
        }
        return roles;
    }

    /**
     * 进入角色授权页面
     */
    @RequestMapping(value = "editRoleRight/{roleId}", method = RequestMethod.GET)
    public String toRoleAuthPage(@PathVariable String roleId) {
        return "system/accessManagement/editRoleRight";
    }

    /**
     * 获取角色权限树数据
     */
    @RequestMapping(value = "getTreeData/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public List<MenuAndFuncTreeNode> getTreeData(@PathVariable String roleId) {
        List<SysMenu> menuList = sysMenuService.findByRoleId(roleId);
        List<SysFunction> funcList = sysFunctionService.findByRoleId(roleId);
        return sysFunctionService.makeTreeData(menuList, funcList);
    }

    /**
     * 进入角色的成员管理页面
     */
    @RequestMapping(value = "editRoleUser/{roleId}", method = RequestMethod.GET)
    public String toRoleUserPage(@PathVariable String roleId, Model model) {
        model.addAttribute("role", sysRoleService.findOne(roleId));
        return "system/accessManagement/roleUserEdit";
    }

    /**
     * 查询该角色管理的用户列表
     */
    @RequestMapping(value = "/{roleId}/users", method = RequestMethod.GET)
    @ResponseBody
    public List<SysUser> findUsersByRoleId(@PathVariable String roleId) {
        return sysRoleService.findUsersByRoleId(roleId);
    }
    /*
    @ApiOperation("查询该系统所有角色组")
    @RequestMapping(method = RequestMethod.GET)
    public ApiResponse<List<SysRole>> findAllRole(@RequestParam(required = false) String typeId) {
        ApiResponse<List<SysRole>> apiResponse = new ApiResponse<>();
        List<SysRole> page = new ArrayList<>();
        if (StringUtils.isEmpty(typeId)) {
            page = sysRoleService.findAll();
        } else {
            page = sysRoleService.findAllSysRole(typeId);
        }
        apiResponse.setData(page);
        return apiResponse;
    }

    @ApiOperation("查询该系统角色管理的用户列表")
    @RequestMapping(value = "/{roleId}/users", method = RequestMethod.GET)
    public ApiResponse<List<SysUser>> findUsersByRoleId(@PathVariable String roleId) {
        ApiResponse<List<SysUser>> apiResponse = new ApiResponse<>();
        List<SysUser> list = sysRoleService.findUsersByRoleId(roleId);
        apiResponse.setData(list);
        return apiResponse;
    }

    @ApiOperation("新增该系统的角色组-点击保存按钮")
    @RequestMapping(value = "/addRole", method = RequestMethod.POST)
    public ApiResponse<Boolean> addRole(@RequestParam String typeId, @RequestBody SysRole sysRole) {
        ApiResponse<Boolean> apiResponse = new ApiResponse<>();
        Boolean result = sysRoleService.addRole(typeId, sysRole);
        apiResponse.setData(result);
        return apiResponse;
    }

    @ApiOperation("删除角色组")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ApiResponse<Boolean> delRole(@PathVariable String id) {
        ApiResponse<Boolean> response = new ApiResponse<>();
        boolean result = this.sysRoleService.deleteRole(id);
        response.setData(result);
        return response;
    }

    @ApiOperation("返回系统功能列表 和 角色的功能权限列表")
    @RequestMapping(value = "/{roleId}/menu", method = RequestMethod.GET)
    public ApiResponse<Map<String, List<SysMenu>>> findFunctions(@PathVariable String roleId) {
        ApiResponse<Map<String, List<SysMenu>>> apiResponse = new ApiResponse<>();
        SysRole role = sysRoleService.findOneRoleWithMenus(roleId);
        List<SysMenu> roleFuncs = new ArrayList();
        if (role != null) {
            roleFuncs = role.getMenuList();
        }
        //查找该角色对应的系统的所有功能菜单
        List<SysMenu> roleTypes = typeService.findAllTypeMenu(roleId);

        Map<String, List<SysMenu>> map = new HashMap<>();
        map.put("cur", roleFuncs);
        map.put("all", roleTypes);

        apiResponse.setData(map);
        return apiResponse;
    }

    @ApiOperation("查询单个角色")
    @RequestMapping(value = "/{roleId}", method = RequestMethod.GET)
    public ApiResponse<SysRole> findOneRole(@PathVariable String roleId) {
        ApiResponse<SysRole> apiResponse = new ApiResponse<>();
        SysRole role = sysRoleService.findOne(roleId);
        apiResponse.setData(role);
        return apiResponse;
    }

    @ApiOperation("删除角色管理的某个用户")
    @RequestMapping(value = "/{roleId}/user/{userId}", method = RequestMethod.DELETE)
    public ApiResponse<List<SysUser>> deleteRoleUserByRoleIdAndUserId(@PathVariable String roleId, @PathVariable String userId) {
        ApiResponse<List<SysUser>> apiResponse = new ApiResponse<>();
        sysRoleService.deleteUserRoleByRoleIdAndUserId(roleId, userId);
        List<SysUser> list = sysRoleService.findUsersByRoleId(roleId);
        apiResponse.setData(list);
        return apiResponse;
    }

    @ApiOperation("从OA获取部门列表")
    @RequestMapping(value = "/oadept", method = RequestMethod.GET)
    public ApiResponse<List<OaDepartmentDO>> listDeptFromOA() {
        ApiResponse<List<OaDepartmentDO>> apiResponse = new ApiResponse<>();
        List<OaDepartmentDO> list = sysRoleService.listDeptFromOA();
        apiResponse.setData(list);
        return apiResponse;
    }

    @ApiOperation("从OA获取部门员工列表")
    @RequestMapping(value = "/oaemp", method = RequestMethod.GET)
    public ApiResponse<List<SysUser>> listDeptEmployeeFromOA(@RequestParam String deptId) {
        ApiResponse<List<SysUser>> apiResponse = new ApiResponse<>();
        List<SysUser> list = sysRoleService.listDeptEmpFromOA(deptId);
        apiResponse.setData(list);
        return apiResponse;
    }

    @ApiOperation("给角色增加单个员工")
    @RequestMapping(value = "/{roleId}", method = RequestMethod.POST)
    public ApiResponse<List<SysUser>> addUser2Role(@PathVariable String roleId, @RequestBody SysUser sysUser) {
        ApiResponse<List<SysUser>> apiResponse = new ApiResponse<>();
        List<SysUser> list = sysRoleService.addUser2Role(roleId, sysUser);
        apiResponse.setData(list);
        return apiResponse;
    }

    @ApiOperation("给角色批量增加员工")
    @RequestMapping(value = "/{roleId}/batch", method = RequestMethod.POST)
    public ApiResponse<List<SysUser>> addUsers2Role(@PathVariable String roleId, @RequestBody List<SysUser> sysUsers) {
        ApiResponse<List<SysUser>> apiResponse = new ApiResponse<>();
        List<SysUser> list = sysRoleService.addUsers2Role(roleId, sysUsers);
        apiResponse.setData(list);
        return apiResponse;
    }

    @ApiOperation("根据工号从OA获取部门员工")
    @RequestMapping(value = "/oaempbyno", method = RequestMethod.GET)
    public ApiResponse<List<SysUser>> listEmployeeFromOA(@RequestParam String workNo) {
        ApiResponse<List<SysUser>> apiResponse = new ApiResponse<>();
        List<SysUser> list = sysRoleService.listEmpFromOAByNo(workNo);
        apiResponse.setData(list);
        return apiResponse;
    }

    @ApiOperation("保存编辑的权限列表")
    @RequestMapping(value = "/{roleId}/editRight", method = RequestMethod.POST)
    public ApiResponse<SysRole> editRight(@PathVariable String roleId, @RequestBody List<SysMenu> menuList) {
        ApiResponse<SysRole> apiResponse = new ApiResponse<>();

        List<String> menuIds = new ArrayList<>();
        List<SysFunc> funcList = new ArrayList<>();
        for (SysMenu obj : menuList) {
            menuIds.add(obj.getId());
            if (obj.getFuncChild() != null && obj.getFuncChild().size() > 0) {
                funcList.addAll(obj.getFuncChild());
            }
        }
        SysRole role = sysRoleService.editRight(roleId, menuIds, funcList);
        apiResponse.setData(role);
        return apiResponse;
    }

    @ApiOperation("根据角色名称模糊匹配返回查询列表")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ApiResponse<List<SysRole>> searchByRoleName(@RequestParam String roleName) {
        ApiResponse<List<SysRole>> apiResponse = new ApiResponse<>();
        List<SysRole> roles = sysRoleService.searchByRoleName(roleName);
        apiResponse.setData(roles);
        return apiResponse;
    }
*/


}
